<?php
/**
 * Create By: PhpStorm
 * User: yihua
 * File: UserCurrency.php
 * Date: 2016/12/26
 * Time: 23:32
 */
namespace app\api\model\dao;

use think\Model;
use think\Db;

class UserCurrency extends Model
{

    protected $table = 'user_currency';
    protected $pk = 'id';
    protected $autoWriteTimestamp = true;

    /**
     * @param $user_id
     * @return array|false
     * 新建账户时，初始化漫豆漫币为0
     */
    public function initCurrency($user_id)
    {
        $data = [
            ['user_id' => $user_id, 'currency_type' => 1, 'amount' => 0],
            ['user_id' => $user_id, 'currency_type' => 2, 'amount' => 0],
        ];
        return $this->saveAll($data);
    }

    /**
     * @param $user_id
     * @param $currency_type
     * @param $amount
     * @param $type 1表示增加，0表示减少
     * @return int|true
     * @throws \think\Exception
     */
    public function changeCurrency($user_id, $currency_type, $amount, $type = 1)
    {
        $conds = [
            'user_id' => $user_id,
            'currency_type' => $currency_type,
        ];
        if ($type) {
            return $this->where($conds)->setInc('amount', $amount);
        } else {
            return $this->where($conds)->setDec('amount', $amount);
        }

        /*
        $sql = sprintf("INSERT INTO %s (user_id,currency_type,amount,create_time,update_time) VALUES (%s,%s,%s,%s,%s)
                ON DUPLICATE KEY UPDATE amount=amount+%s", $this->table, $user_id, $currency_type, $amount, time(), time(), $amount);
        return Db::execute($sql);
        */
    }

    /**
     * 扣除漫币 | 漫豆
     *
     * @param $user_id
     * @param $currency_type
     * @param $amount
     * @return int
     */
    public function deductCurrency($user_id, $currency_type, $amount)
    {
        $sql = sprintf("update %s set amount=amount-%d,update_time=%d where user_id=%d and currency_type=%d", $this->table, $amount, time(), $user_id, $currency_type);
        return Db::execute($sql);
    }

    /**
     * @param $user_id
     * @param $currency_type
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getCurrencyByUserid($user_id, $currency_type)
    {
        $conds = array(
            'user_id' => $user_id,
            'currency_type' => ['=', $currency_type],
        );
        $field = array(
            'user_id',
            'currency_type',
            'amount',
            //'update_time'
        );
        return $this->where($conds)->field($field)->find();
    }

    /**
     * @param $user_id
     * @param $currency_type
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getCurrencyByUserids($userids, $currency_type)
    {
        $conds = array(
            'user_id' => ['in', $userids],
            'currency_type' => ['=', $currency_type],
        );
        $field = array(
            'user_id',
            'amount',
        );
        return $this->where($conds)->field($field)->select();
    }

    public function getSumAmountByUserid($conds = [], $filed = 'amount')
    {
        $this->allowField(true);
        $arrCond = [];
        foreach ($conds as $key => $val) {
            if (in_array($key, $this->field)) {
                $arrCond[$key] = $val;
            }
        }
        empty($arrCond) && $arrCond = '1=1';
        return Db::table($this->table)->where($conds)->sum($filed);
    }

    public function getUpdateTimeAttr($value)
    {
        return date('Y-m-d H:m:s', $value);
    }

    /*
        public function getCurrencyTypeAttr($value)
        {
            $status = [
                1 => '漫币',
                2 => '漫豆',
            ];
            return $status[$value];
        }
    */

}
